package com.tencent.compose.map.route.multiPlan.carRoutePreference

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import kotlinx.coroutines.delay
import androidx.compose.foundation.Image
import kotlinproject.composeapp.generated.resources.Res
import kotlinproject.composeapp.generated.resources.img
import kotlinproject.composeapp.generated.resources.route_option_tip_back
import org.example.project.R

// --------- Mock ViewModel 简单版 ---------
class CarRoutePreferenceViewModel {
    var rememberSelect = mutableStateOf(false)
    var showPopupWindow = mutableStateOf(false)

    fun onRememberSelectEvent() {
        rememberSelect.value = !rememberSelect.value
    }
}

// --------- 组件入口 ---------
@Composable
fun CarRoutePreferenceBottomToolView(
    viewModel: CarRoutePreferenceViewModel = remember { CarRoutePreferenceViewModel() }
) {
    val isSelected = viewModel.rememberSelect.value
    var backgroundColor = Color.Black.copy(alpha = 0.08f)
    var textColor = Color.Black
    var iconRes = android.R.drawable.ic_input_add // 随便用系统的图片代替

    val density = LocalDensity.current
    val popupOffsetY =
        with(density) { (10.dp + 28.dp + 10.dp + 94.dp + 20.dp - 44.dp).toPx() }.toInt()

    if (isSelected) {
        backgroundColor = Color(0xFF1D73FB).copy(alpha = 0.1f)
        textColor = Color(0xFF1D73FB)
        iconRes = android.R.drawable.checkbox_on_background // 选中状态
    }

    Row(
        modifier = Modifier.fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center,
    ) {
        Box(
            modifier = Modifier.weight(1f),
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.Center,
            ) {
                Row(
                    modifier = Modifier
                        .size(78.dp, 20.dp)
                        .clip(RoundedCornerShape(10.dp))
                        .background(backgroundColor)
                        .clickable { viewModel.onRememberSelectEvent() },
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center,
                ) {
                    Image(
                        painter = painterResource(id = iconRes),
                        contentDescription = "Icon",
                        modifier = Modifier.size(12.dp)
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text("记住选项", color = textColor, fontSize = 12.sp)
                }

                Spacer(modifier = Modifier.width(4.dp))

                Image(
                    painter = painterResource(id = android.R.drawable.ic_dialog_info),
                    contentDescription = "Info",
                    modifier = Modifier
                        .size(16.dp)
                        .clickable {
                            viewModel.showPopupWindow.value = !viewModel.showPopupWindow.value
                        }
                )
            }
        }
    }

    // 弹出 Popup 提示
    if (viewModel.showPopupWindow.value) {
        Popup(
            onDismissRequest = { viewModel.showPopupWindow.value = false },
            properties = PopupProperties(focusable = true),
            offset = IntOffset(8, popupOffsetY),
        ) {
            Box(
                modifier = Modifier
                    .width(210.dp)
                    .height(44.dp)
            ) {
                // 背景图
                Image(
//                    painter = painterResource(id = R.drawable.route_option_tip_back),
                    painter = org.jetbrains.compose.resources.painterResource(Res.drawable.route_option_tip_back),

                    contentDescription = "Tip Background",
                    modifier = Modifier
                        .fillMaxSize(),
                    contentScale = ContentScale.FillBounds
                )

                // 文字和关闭按钮
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(32.dp),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center,
                ) {
                    Spacer(modifier = Modifier.width(2.dp))
                    Text(
                        "勾选后每次导航都会使用该偏好",
                        color = Color.White,
                        fontSize = 12.sp,
                        maxLines = 1,
                    )
                    Spacer(modifier = Modifier.width(6.dp))
                    Box(
                        modifier = Modifier
                            .size(0.5.dp, 20.dp)
                            .background(Color.White.copy(alpha = 0.2f))
                    )
                    Spacer(modifier = Modifier.width(6.dp))

                    // 关闭按钮也可以用资源
                    Image(
                        painter = org.jetbrains.compose.resources.painterResource(Res.drawable.img),

                        contentDescription = "Close Tip",
                        modifier = Modifier
                            .size(16.dp)
                            .clickable {
                                viewModel.showPopupWindow.value = false
                            }
                    )
                    Spacer(modifier = Modifier.width(6.dp))
                }
            }
        }

        // 自动5秒后关闭
        LaunchedEffect(Unit) {
            delay(5000)
            viewModel.showPopupWindow.value = false
        }
    }
}